package org.app.format;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.app.Ports;
import org.app.data.MyBean;

public abstract class WeSerialized {

    
   
    /**
     * 加密
     * @param t
     * @return
     */
    public <T> StringBuffer decrypt(T t) {
		StringBuffer sb = new StringBuffer();
		if (t == null)
			return sb;
		if (t instanceof Number) {
			sb.append(byNumber((Number)t));
		} else if (t instanceof String) {
			sb.append(byString(t.toString()));
		} else if (t.getClass().getClassLoader() != null) {// 用户自定义类型
			Map map = this.toMap(t);
			sb.append(decrypt(map));
		} else if (t instanceof Map) {
			
			Map map = (Map) t;
			Set<Entry> set = map.entrySet();
			Object[] format=new Object[map.size()];
			int id=0;
			
			for (Entry<String,Object> entry : set) {
				Object value = entry.getValue();
				String key=entry.getKey();
				if (value== null||"".equals(value)) {
					continue;
				}
				
				if(key.equals("class")){//
					format[format.length-1]=value;
				}else{
			
				format [id]=byMap(key,decrypt(value));
				id++;
				}
				
			}
			sb.append(byMap(format));
		} else 
			
			//关于数组，长度+1,下标[0]保存其类型 List/Vector/Object[] 等
		if (t instanceof List) {
			List list = (List) t;
			int size = list.size();
			if (size <= 0)
				return sb;

			Object[] format = new Object[size+1];
			 format[size]=t.getClass();
			for (int i = 0; i < size; i++) {
				format[i] = decrypt(list.get(i)).toString();
			}
           
            sb.append(byArray(format));
		}else 
		if (t.getClass().isArray()) {
			int size = Array.getLength(t);
			if (size <= 0)
				return sb;
			Object[] format = new Object[size+1];
			format[size]=t.getClass(); /////////bug 修改了数组顺序
			for (int i = 0; i < size; i++) {
				format[i] = decrypt(Array.get(t, i)).toString();
			}

			// sb.setCharAt(sb.length()-1, parallelRight);
			sb.append(byArray(format));
		}else{
			sb.append(t);
		}
		return sb;
	}
    
    public abstract String byNumber(Object n);
    public abstract String byString(String s);
    public abstract String byMap(Object[] values);
    public abstract String byMap(String key,StringBuffer value);
    public abstract String byArray(Object[] values);
  
    private <T> Map toMap(final T t) {
		final Map map = new HashMap();
		// System.out.println(t);
		MyBean.visit(t, new Ports() {

			@Override
			public Object execute(Object obj) {
				// TODO Auto-generated method stub
				PropertyDescriptor pd = (PropertyDescriptor) obj;
				Class type = pd.getPropertyType();
				String name = pd.getName();
//				if (name.equals("class"))
//					return null;
				try {
					map.put(pd.getName(), pd.getReadMethod().invoke(t));
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return null;
			}
		});
		return map;
	}
    
    
    public Object encript(String temp){
    	return null;
    }
 
}
